﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using System.Web.Mvc;

namespace LeadVinaglue.Repository
{
    public class PaymentManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public PaymentManagementModel GetModel(string pageType, string roleName, int id)
        {
            PaymentManagementModel model = new PaymentManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListPayment = GetAll();

            if (id > 0)
            {
                model.PaymentInfo = GetModelItem(id);
            }

            return model;
        }

        public PaymentManagementModel GetModel(string pageType, string roleName)
        {
            PaymentManagementModel model = new PaymentManagementModel();
            model.ListPayment = GetAll();

            return model;
        }

        public List<PaymentModel> GetAll(bool group)
        {
            List<PaymentModel> list = new List<PaymentModel>();

            var items = (from c in entities.PaymentMethods
                         orderby c.DateCreated descending
                         select c).ToList();

            if (group)
            {
                list.Add(new PaymentModel { ID = -1, Name = "All" });
            }

            foreach (var item in items)
            {
                PaymentModel model = new PaymentModel();

                model.ID = item.ID;
                model.Name = item.Name;
                model.Code = item.Code == null ? string.Empty : item.Code;
                model.Vendor = item.Vendor == null ? string.Empty : item.Vendor;
                model.LinkUrl = item.LinkURL == null ? string.Empty : item.LinkURL; 
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Active = item.Active;

                list.Add(model);
            }

            return list;
        }

        public List<PaymentModel> GetAll()
        {
            List<PaymentModel> list = new List<PaymentModel>();

            var items = (from c in entities.PaymentMethods
                         orderby c.DateCreated descending
                         select c).ToList();                       

            foreach (var item in items)
            {
                PaymentModel model = new PaymentModel();

                model.ID = item.ID;
                model.Name = item.Name;
                model.Code = item.Code == null ? string.Empty : item.Code;
                model.Vendor = item.Vendor == null ? string.Empty : item.Vendor;
                model.LinkUrl = item.LinkURL == null ? string.Empty : item.LinkURL; 
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Active = item.Active;

                list.Add(model);
            }

            return list;
        }

        public PaymentModel GetModelItem(int id)
        {
            PaymentModel model = new PaymentModel();
            var item = (from _object in entities.PaymentMethods where _object.ID == id select _object).FirstOrDefault();

            if (item != null)
            {
                model.ID = item.ID;
                model.ID = item.ID;
                model.Name = item.Name;
                model.Code = item.Code;
                model.Vendor = item.Vendor;
                model.LinkUrl = item.LinkURL;
                model.Active = item.Active;
            }

            return model;
        }

        public PaymentMethod GetSingle(int id)
        {
            return (from _object in entities.PaymentMethods where _object.ID == id select _object).FirstOrDefault();
        }

        public PaymentMethod GetSingle(string name)
        {
            return (from _object in entities.PaymentMethods where _object.Name == name select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] PaymentMethod _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToPaymentMethods(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.PaymentMethods where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] PaymentMethod _object)
        {
            _object.EntityKey = (from PaymentMethod ac in entities.PaymentMethods where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}